package com.chj.zhongji.class01.test;

public class Code04_ColorLeftRight {

	// RGRGR -> RRRGG
	public static int minPaint(String s) {
		if (s == null || s.length() < 2) {
			return 0;
		}

		char[] ss = s.toCharArray();
		int[] right = new int[ss.length];

//		int res = 0;
//		for (int i = 0; i < right.length; i++) {
//			int rr = ss[ss.length - 1 - i] == 'R' ? 1 : 0;
//			res += rr;
//			right[right.length - 1 - i] = res;
//		}

		right[ss.length - 1] = ss[ss.length - 1] == 'R' ? 1 : 0;
		for (int i = ss.length - 2; i >= 0; i--) {
			right[i] = right[i + 1] + (ss[i] == 'R' ? 1 : 0);
		}

//		for (int i = 0; i < right.length; i++) {
//			System.out.println(right[i]);
//		}

		int res = right[0];
		int left = 0;

		for (int i = 0; i < ss.length - 1; i++) {
			left += ss[i] == 'G' ? 1 : 0;
			res = Math.min(res, left + right[i + 1]);
		}

		res = Math.min(res, left + (ss[ss.length - 1] == 'G' ? 1 : 0));
		return res;
	}

	public static void main(String[] args) {
		String test = "GGGGGR";
//		String test = "RGRGR";
		System.out.println(minPaint(test));

	}

}
